package com.artfii;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

public class DateUtil {


    private static DateTimeFormatter formatter;

    static {
        DateTimeParser[] pattern = {
            DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSSZ").getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").getParser(),
            DateTimeFormat.forPattern("yyyyMMdd").getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd").getParser(),
            DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").getParser(),
            DateTimeFormat.forPattern("yyyy/MM/dd").getParser(),
            DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").getParser(),
            DateTimeFormat.forPattern("yyyy/MM/dd'T'HH:mm:ss.SSSZ").getParser(),
            DateTimeFormat.forPattern("yyyyMMddHHmmss").getParser(),
            DateTimeFormat.forPattern("yyyyMMddHHmmssZ").getParser(),
            DateTimeFormat.forPattern("EEE, d MMM yyyy HH:mm:ss Z").getParser(),
            DateTimeFormat.forPattern("MMM d, yyyy HH:mm:ss a").getParser(),
            DateTimeFormat.forPattern("EEE, MMM d, ''yy").getParser(),
            //在此增加新的日期 pattern
        };
        formatter = new DateTimeFormatterBuilder().append(null, pattern).toFormatter();
    }

    /**
     * 把常见的日期字串转日期
     *
     * @param dateStr
     * @return
     * @author:lqf
     */
    public static Date ToDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) return null;
        if (StringUtils.equals("0000-00-00 00:00:00", dateStr)) return null;

        try {
            return formatter.parseDateTime(dateStr).toDate();
        } catch (IllegalArgumentException e) {
            if (isLongOfTimestamp(dateStr)) {
                return new Date(NumberUtils.toLong(dateStr));
            }
        }
        logger.error("日期解析出错:{}", dateStr);
        return null;
    }

    public static DateTime ToJodaDate(String dateStr) {
        return formatter.parseDateTime(dateStr);
    }

    private static boolean isLongOfTimestamp(String dataStr){
        return NumberUtils.isNumber(dataStr) && 10 <= dataStr.length();
    }

    public static String formatStringyyyyMMdd(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    protected static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 解析字符串为日期（可补充）
     *
     * @param str
     * @return
     */
    public static Date parse(String str) {
        if (null == str || 0 == str.length()) {
            return null;
        }
        DateTime dateTime = null;
        try {
            dateTime = DateTime.parse(str);
            return dateTime.toDate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isDate(String str) {
        if (null == str || 0 == str.length()) {
            return false;
        }
        return null == DateTime.parse(str) ? false : true;
    }

    /** 判断日期是否有效且提前/延后天数为今天 */
    public static boolean isToday(String strDate, int days) {
        return isToday(strDate, days, "yyyy-MM-dd");
    }

    public static boolean isToday(String strDate, int days, String fmt) {
        if(isValidDate(strDate, fmt)){
            String nowStrDate = new DateTime().toString(fmt);
            String realDate = formatDate(getDateAfter(getDate(strDate), days));
            return compareDate(nowStrDate, realDate, fmt) == 0;
        } else {
            return false;
        }
    }

    /** 判断日期是否有效且为今天 */
    public static boolean isToday(String strDate) {
        return isToday(strDate, "yyyy-MM-dd");
    }

    public static boolean isToday(String strDate, String fmt) {
        String nowStrDate = new DateTime().toString(fmt);
        return isValidDate(strDate, fmt) && compareDate(nowStrDate, strDate, fmt) == 0;
    }

    /** 判断字符串是不是有效日期格式 */
    // 假设传入的日期格式是yyyy-MM-dd HH:mm:ss, 也可以传入yyyy-MM-dd，如2018-1-1或者2018-01-01格式
    public static boolean isValidDate(String strDate) {
        return isValidDate(strDate, "yyyy-MM-dd");
    }
    public static boolean isValidDate(String strDate, String fmt) {
        SimpleDateFormat format = new SimpleDateFormat(fmt);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2018-02-29会被接受，并转换成2018-03-01
            format.setLenient(false);
            format.parse(strDate);
            // 判断传入的yyyy年-MM月-dd日 字符串是否为数字
            /*String[] sArray = strDate.split("-");
            for (String s : sArray) {
                boolean isNum = s.matches("[0-9]+");
                // +表示1个或多个（如"3"或"225"），*表示0个或多个（[0-9]*）（如""或"1"或"22"），?表示0个或1个([0-9]?)(如""或"7")
                if (!isNum) {
                    return false;
                }
            }*/
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return false;
        }
        return true;
    }

    public static long getHMS(String time) {
        if (time != null && time.length() > 0) {
            StringTokenizer token = new StringTokenizer(time, ":");

            int hourTime = Integer.parseInt(token.nextToken()) * 3600;
            int minute = Integer.parseInt(token.nextToken()) * 60;
            int second = Integer.parseInt(token.nextToken());

            return hourTime + minute + second;
        }
        return 0l;
    }

    public static String formatLong(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 下个月第一天
     *
     * @param date
     * @return
     */
    public static Date afterMonthFirstDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 上个月第一天
     *
     * @param date
     * @return
     */
    public static Date perMonthFirstDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:31:18
     * @description 本月第一天
     */
    public static Date thisMonthFirstDate(Date date) {
        int month = DateUtil.getMonth(date);
        int year = DateUtil.getYear(date);
        String strDate = year + "-" + month + "-" + "1";
        return getDate(strDate);
    }


    /**
     * @param date
     * @return congge 2012-2-15下午05:31:18
     * @description 前一个月第一天
     */
    public static Date preMonthFirstDate(Date date) {
        int month = DateUtil.getMonth(date);
        int year = DateUtil.getYear(date);
        month--;
        if (month == 0) {
            month = 12;
            year--;
        }
        String strDate = year + "-" + month + "-" + "1";
        return getDate(strDate);
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:31:32
     * @description 前一个月最后一天
     */
//    public static Date preMonthLastDate(Date date) {
//        int month = DateUtil.getMonth(date);
//        int year = DateUtil.getYear(date);
//        month--;
//        if (month == 0) {
//            month = 12;
//            year--;
//        }
//        int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//        String strDate = year + "-" + month + "-" + daysInMonth[month+1];
//        return getDate(strDate);
//    }
    /**
     * @param date
     * @return xiell
     * @description 前一个月最后一天
     */
    public static Date preMonthLastDate(Date date) {
        DateTime dateTime = new DateTime(date);
        String strDate = dateTime.minusMonths(1).dayOfMonth().withMaximumValue().toString("yyyy-MM-dd");
        return getDate(strDate);
    }

    /**
     * @param value
     * @param patten
     * @return congge 2012-2-15下午05:31:56
     * @description 日期转换 字符串 转为 date
     */
    public static Date getDate(String value, String patten) {
        Date date = null;
        if (value == null) {
            return new Date();
        }
        SimpleDateFormat fordate = new SimpleDateFormat(patten);
        try {
            date = fordate.parse(value);
        } catch (ParseException e) {
            logger.error("日期转换  字符串 转为 date 出错", e);
        }
        return date;
    }

    /**
     * 转换时间格式 yyyy-MM-dd HH:mm
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String formatStringyyyyMMddhhmm(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String formatStringyMd(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 转换时间格式 yyyy-MM-dd HH:mm:ss
     */
    public static String formatStringyyyyMMddhhmmss(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * @param value
     * @return 形如yyyy-MM-dd HH:mm:ss congge 2012-2-15下午05:32:40
     * @description 日期时间转换
     */
    public static Date getTime(String value) {
        Date date = null;
        if (value == null) {
            return null;
        }
        SimpleDateFormat fordate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = fordate.parse(value);
        } catch (ParseException e) {
            logger.error("日期时间转换 出错", e);
        }
        return date;
    }

    /**
     * @param value
     * @return congge 2012-2-15下午05:33:21
     * @description 日期转换 形如 yyyy-MM-dd
     */
    public static Date getDate(String value) {
        Date date = null;
        if (StringUtils.isEmpty(value)) {
            return new Date();
        }
        SimpleDateFormat fordate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = fordate.parse(value);
        } catch (ParseException e) {
            logger.error("日期转换  形如 	yyyy-MM-dd 出错", e);
            return null;
        }
        return date;
    }

    /**
     * @return congge 2012-2-15下午05:33:39
     * @description 获取当前时间
     */
    public static String getNowTime() {
        Date date = new Date();
        SimpleDateFormat fordate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return fordate.format(date);
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:45:30
     * @description 将 date 型日期转换为 yyyy-MM-dd HH:mm:ss String 型
     */
    public static String getStrTimeByDate(Date date) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat fordate = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
            return fordate.format(date);
        } catch (Exception e) {
            logger.error("将 date 型日期转换为 yyyy-MM-dd HH:mm:ss String 出错", e);
        }
        return null;
    }

    /**
     * 将Date转换为yyyy-MM
     *
     * @param date
     * @return
     */
    public static String getStrYeraMonByDate(Date date) {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat fordate = new SimpleDateFormat(
                "yyyy-MM");
            return fordate.format(date);
        } catch (Exception e) {
            logger.error("将 date 型日期转换为 yyyy-MM String 出错", e);
        }
        return null;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:34:01
     * @description 前一天
     */
    public static Date preDate(Date date) {
        long time = date.getTime();
        time = time - 24 * 60 * 60 * 1000;
        Date ytd = new Date(time);
        return ytd;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:34:01
     * @description 前一天转换成yyyy-MM-dd
     */
    public static String getPreDateStr(Date date) {
        long time = date.getTime();
        time = time - 24 * 60 * 60 * 1000;
        Date ytd = new Date(time);
        SimpleDateFormat fordate = new SimpleDateFormat("yyyy-MM-dd");
        return fordate.format(ytd);
    }

    /**
     * @return congge 2012-2-15下午05:34:10
     * @description 昨天
     */
    public static Date yesterday() {
        Date now = new Date();
        long time = now.getTime();
        time = time - 24 * 60 * 60 * 1000;
        Date ytd = new Date(time);
        return ytd;

    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:34:16
     * @description 某天 的明天
     */
    public static Date nextDay(Date date) {
        long time = date.getTime();
        time = time + 24 * 60 * 60 * 1000;
        Date tomorrow = new Date(time);
        return tomorrow;
    }

    /**
     * @param date
     * @param count
     * @return congge 2012-2-15下午05:34:31
     * @description 某时间的 几天后的时间
     */
    public static Date nextSomeDayDate(Date date, int count) {
        long time = date.getTime();
        time = time + 24 * 60 * 60 * 1000 * count;
        Date someDay = new Date(time);
        return someDay;
    }

    /**
     * @param date
     * @param preMinute
     * @return congge 2012-2-15下午05:34:57
     * @description 某时间 的 多少分钟前 的时间
     */
    public static Date preDateMinute(Date date, int preMinute) {
        long time = date.getTime();
        time = time - 60 * 1000 * preMinute;
        Date preTime = new Date(time);
        return preTime;
    }

    /**
     * @param date
     * @param delayMinute
     * @return congge 2012-3-27下午01:27:43
     * @description 某时间的多少分钟后的时间
     */
    public static Date nextDateMinute(Date date, int delayMinute) {
        long time = date.getTime();
        time = time + 60 * 1000 * delayMinute;
        Date delayTime = new Date(time);
        return delayTime;
    }

    /**
     * 某时间多少秒之前的时间
     *
     * @param date
     * @param beforeSecond
     * @return hongjunqi 2015年1月29日下午4:32:41
     * @description
     */
    public static Date beforeDateSecond(Date date, int beforeSecond) {
        long time = date.getTime();
        time = time - 1000 * beforeSecond;
        Date delayTime = new Date(time);
        return delayTime;
    }

    /**
     * @param date       目标时间
     * @param delySecond 相隔秒数
     * @return 目标时间之后的时间 congge 2012-9-4上午11:30:03
     * @description 某时间的多少秒后的时间
     */
    public static Date nextDateSecond(Date date, int delySecond) {
        long time = date.getTime();
        time = time + 1000 * delySecond;
        Date delayTime = new Date(time);
        return delayTime;
    }

    /**
     * @return congge 2012-2-15下午05:42:17
     * @description 明天
     */
    public static Date tomorrow() {
        Date now = new Date();
        long time = now.getTime();
        time = time + 24 * 60 * 60 * 1000;
        Date tomorrow = new Date(time);
        return tomorrow;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:42:29
     * @description 格式化 日期 yyyy-MM-dd
     */
    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * @param date
     * @param formater
     * @return congge 2012-2-15下午05:42:41
     * @description 格式化日期
     */
    public static String formatDate(Date date, String formater) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(formater);
        if (date == null) {
            return null;
        } else {
            String datestring = bartDateFormat.format(date);
            return datestring;
        }
    }

    /**
     * @return congge 2012-2-15下午05:42:59
     * @description 格式化日期 yyyy.MM
     */
    public static String getYearMonth() {
        String dateStr = formatDate(new Date(), "yyyy.MM");
        return dateStr;
    }

    /**
     * @param format
     * @return congge 2012-2-15下午05:43:02
     * @description
     */
    public static String getFormatDate(String format) {
        String dateStr = formatDate(new Date(), format);
        return dateStr;
    }

    /***
     * @description 获取 年字符串
     * @param date
     * @return congge 2012-2-15下午05:37:41
     */
    public static String getYearStr(Date date) {
        String a = formatDate(date, "yyyy");
        return a;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:37:53
     * @description 获取 年.. 整形
     */
    public static int getYear(Date date) {
        String a = formatDate(date, "yyyy");
        int b = Integer.parseInt(a);
        return b;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:38:03
     * @description 获取 天字符串
     */
    public static String getDayStr(Date date) {
        String a = formatDate(date, "dd");
        return a;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:38:40
     * @description 获取 天 整形
     */
    public static int getDay(Date date) {
        String a = formatDate(date, "dd");
        int b = Integer.parseInt(a);
        return b;
    }

    /***
     * @description 获取 小时 整数型
     * @param date
     * @return congge 2012-2-15下午05:38:58
     */
    public static int getHour(Date date) {
        String a = formatDate(date, "HH");
        int b = Integer.parseInt(a);
        return b;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:39:14
     * @description 获取 月 字符串
     */
    public static String getMonthStr(Date date) {
        String a = formatDate(date, "MM");
        return a;
    }

    /**
     * @param date
     * @return congge 2012-2-15下午05:39:33
     * @description 获取 月 整数型
     */
    public static int getMonth(Date date) {
        String a = formatDate(date, "MM");
        int b = Integer.parseInt(a);
        return b;
    }

    /**
     * @param d
     * @param day
     * @return congge 2012-2-15下午05:37:04
     * @description 某天后 的几天
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * @param d
     * @param day
     * @return congge 2012-2-15下午05:37:15
     * @description 某天前的几天
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();

    }

    /**
     * 取得当前时间
     *
     * @return: yyyyMMdd HHmmss
     */
    public static String getDateTimeForStr(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(date);
    }

    /**
     * 取得当前时间
     *
     * @return: yyyyMMdd
     */
    public static String getDateTime(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(date);
    }


    /**
     * 两个时间之间 相间隔的天数
     *
     * @param start
     * @param end   congge 2012-2-15下午05:36:37
     * @description
     */
    public static Integer between(Date start, Date end) {
        if (start.getTime() >= end.getTime()) {
            return 0;
        }
        int i = 1;
        start = DateUtil.getDate(DateUtil.formatDate(start));
        end = DateUtil.getDate(DateUtil.formatDate(end));
        while (start.getTime() < end.getTime()) {
            start.setTime(start.getTime() + 24 * 60 * 60 * 1000);
            i++;
        }
        return i;
    }

    /**
     * @param startDate
     * @param endDate   congge 2012-2-15下午05:55:55
     * @description 两时间间隔月份数
     */
    public static int minusMonths(Date startDate, Date endDate) {
        long months = (endDate.getTime() - startDate.getTime())
            / (30 * 24 * 60 * 60 * 1000);
        return Integer.parseInt(Long.toString(months));
    }

    /**
     * @param startDate
     * @param endDate
     * @return congge 2012-2-15下午05:56:37
     * @description 两时间间隔的天数
     */
    public static int minusDays(Date startDate, Date endDate) {
        long times = (endDate.getTime() - startDate.getTime())
            / (24 * 60 * 60 * 1000);
        return Integer.parseInt(Long.toString(times));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @param date1 前面小时间
     * @param date2 后面大时间 congge 2012-4-23下午04:16:20
     * @description 两个时间 间隔的分钟数
     */
    public static int minusMinutes(Date date1, Date date2) {
        long times = (date2.getTime() - date1.getTime()) / (60 * 1000);
        return Integer.parseInt(Long.toString(times));
    }

    /**
     * @param date1 前面小时间
     * @param date2 后面大时间 congge 2012-4-23下午04:16:20
     * @description 两个时间 间隔的分钟数
     */
    public static int minusMinutes(long date1, long date2) {
        long times = (date2 - date1) / (60 * 1000);
        return Integer.parseInt(Long.toString(times));
    }

    /**
     * @param startDate
     * @param endDate
     * @return congge 2012-2-15下午05:59:04
     * @description 两 时间间隔的周数
     */
    public static int minusWeeks(Date startDate, Date endDate) {
        return minusDays(startDate, endDate) / 7;
    }

    /**
     * 获取某时间 在本周是第几天
     *
     * @param date Date
     * @return int
     */
    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date == null)
            date = new Date();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /***
     * @description 返回 时间 是本周第几天
     * @param date
     * @return "一,二" congge 2012-2-15下午06:04:36
     */
    public static String getDayOfWeekStr(Date date) {
        Calendar calendar = Calendar.getInstance();
        if (date == null)
            date = new Date();
        calendar.setTime(date);
        int tmp = calendar.get(Calendar.DAY_OF_WEEK);
        String str = "";
        switch (tmp) {
            case 1:
                str = "日";
                break;
            case 2:
                str = "一";
                break;
            case 3:
                str = "二";
                break;
            case 4:
                str = "三";
                break;
            case 5:
                str = "四";
                break;
            case 6:
                str = "五";
                break;
            case 7:
                str = "六";
                break;
        }
        return str;
    }

    // 必须是类似于12/01/09 12:11
    public static Date getTimeByStr(String value) {
        Date result = null;
        try {
            String zz = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{2}//s*[0-9]{1,2}:[0-9]{1,2}";
            String zzS = "[0-9]{4}";
            if (value == null || value.equals(""))
                return null;
            int index = value.indexOf(" ");
            String time = value.substring(index + 1);
            String date = value.substring(0, index);
            String days[] = date.split("/");
            String timeStr = "20" + days[2] + "-" + days[0] + "-" + days[1]
                + " " + time + ":00";
            result = DateUtil.getDate(timeStr, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            logger.error("必须是类似于12/01/09 12:11  出错", e);
        }
        return result;
    }

    /**
     * @param nowdate
     * @param delay   congge 2011-12-19上午09:06:01
     * @description 得到一个时间延后或前移几天的时间, nowdate为时间, delay为前移或后延的天数
     */
    public static String getNextDay(String nowdate, String delay) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            // Date d = strToDate(nowdate);
            Date d = format.parse(nowdate);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24
                * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            logger.error("得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数  出错", e);
            return "";
        }
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 获取某一周指定的周几的时间 如上一周周三：week = -1,weekDay = 4(周一为第一天)
     *
     * @param weekDay
     * @param week
     * @throws Exception
     */
    public static String getOneDayOfWeek(int week, int weekDay)
        throws Exception {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.WEEK_OF_YEAR, week);
        int preNowWeekDay = c.get(Calendar.DAY_OF_WEEK);
        int preSubDays = weekDay - preNowWeekDay + 1;
        c.add(Calendar.DAY_OF_WEEK, preSubDays);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(c.getTime());
    }

    /**
     * 获取一天的开始时间
     */
    public static Date getDayStart() {
        Calendar currentDate = new GregorianCalendar();
        currentDate.set(Calendar.HOUR_OF_DAY, 0);
        currentDate.set(Calendar.MINUTE, 0);
        currentDate.set(Calendar.SECOND, 0);
        Date d = (Date) currentDate.getTime().clone();
        return d;
    }

    /**
     * 获取一天的结束时间
     *
     * @return
     */
    public static Date getDayEnd() {
        Calendar currentDate = new GregorianCalendar();
        currentDate.set(Calendar.HOUR_OF_DAY, 23);
        currentDate.set(Calendar.MINUTE, 59);
        currentDate.set(Calendar.SECOND, 59);
        Date d = (Date) currentDate.getTime().clone();
        return d;
    }

    /**
     * @param date
     * @return 指示当前年中的星期数
     */
    public static int getWeekOfYear(Date date) {
        if (date == null)
            date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 判断是否是否当天
     *
     * @param inDate
     * @return yanping 2012-5-28上午12:57:58
     * @description
     */
    public static boolean isToday(Date inDate) {
        Calendar now = Calendar.getInstance();
        Calendar inc = Calendar.getInstance();
        inc.setTime(inDate);
        return now.get(Calendar.YEAR) == inc.get(Calendar.YEAR)
            && now.get(Calendar.MONTH) == inc.get(Calendar.MONTH)
            && now.get(Calendar.DAY_OF_MONTH) == inc
            .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 比较两个时间是否相等
     *
     * @param d1 时间参数1
     * @param d2 时间参数2
     * @return 是否相等
     */
    public static boolean compareTwoDate(Date d1, Date d2) {
        return d1.equals(d2);
    }

    /**
     * 获取当前季节
     */
    public static int getCurrentSeason() {
        Calendar c = Calendar.getInstance();
        int month = c.get(Calendar.MONTH);
        if (month >= 0 && month < 3) {
            return 1;
        } else if (month > 3 && month < 6) {
            return 2;
        } else if (month > 6 && month < 9) {
            return 3;
        }
        return 4;
    }

    /**
     * @param date  Date 要比较的时间
     * @param begin Date 开始时间
     * @param end   Date 结束时间
     * @return boolean true表示在开始和结束时间之间
     */
    public static boolean getBetweenTime(Date date, Date begin, Date end) {
        if (date == null || begin == null || end == null) {
            return false;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar beginDate = Calendar.getInstance();
        beginDate.setTime(begin);
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(end);
        int month = cal.get(Calendar.MONTH) + 1;
        int beginMonth = beginDate.get(Calendar.MONTH) + 1;
        int endMonth = endDate.get(Calendar.MONTH) + 1;
        if (beginMonth == month || endMonth == month) {
            return true;
        } else return beginMonth < month && month < endMonth;
    }

    /**
     * 获取当天最大时间
     *
     * @return 返回如下格式yyyy-MM-dd hh:mm:ss eg:2012.01.01 10:10:10 yanping
     * 2012-4-28下午12:03:40
     * @description
     */
    public static String getMaxTimeOfToday() {
        // DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // String ymd = sdf.format(Calendar.getInstance().getTime());
        // return ymd + " 23:59:59";
        return getMaxTimeByDay(Calendar.getInstance().getTime());
    }

    /**
     * @param date 日期
     * @return yanping 2012-5-12下午2:00:20
     * @description 根据日期获取最大时间
     */
    public static String getMaxTimeByDay(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date) + " 23:59:59";
    }

    /**
     * 获取当天最小时间
     *
     * @return yanping 2012-4-28下午12:03:30
     * @description
     */
    public static String getMinTimeOfToday() {
        // DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // String ymd = sdf.format(Calendar.getInstance().getTime());
        // return ymd + " 00:00:00";
        return getMinTimeByDay(Calendar.getInstance().getTime());
    }

    /**
     * @param date 日期
     * @return yanping 2012-5-12下午2:00:57
     * @description 根据日获取最小时间
     */
    public static String getMinTimeByDay(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date) + " 00:00:00";
    }


    /**
     * @param Date1
     * @param Date2
     * @return qc 2014年2月21日下午1:50:35
     * @description 比较时间
     */
    public static int compareDate(String Date1, String Date2) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        try {
            Date dt1 = df.parse(Date1);
            Date dt2 = df.parse(Date2);
            return compareDate(dt1, dt2);
        } catch (Exception exception) {
        }
        return 0;
    }

    /**
     * 比较时间
     *
     * @param Date1
     * @param Date2
     * @param format 格式
     * @return
     */
    public static int compareDate(String Date1, String Date2, String format) {

        DateFormat df = new SimpleDateFormat(format);

        try {
            Date dt1 = df.parse(Date1);
            Date dt2 = df.parse(Date2);
            return compareDate(dt1, dt2);
        } catch (Exception exception) {
        }
        return 0;
    }

    public static int compareDate(Date dt1, Date dt2) {
        try {
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
        }
        return 0;
    }

    /**
     * @param date
     * @return
     * @Description: 获取指定日期所在月的最后一天
     * @author pangjianhai
     * @date 2014-8-19 下午2:05:18
     * @Date
     */
    public static Date lastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * yyyy-MM 格式日期转为该月第一天时间
     *
     * @param str
     * @return
     */
    public static Date formatDateByYearMon(String str) {//yyyy-MM
        str = str + "-01  00:00:00";
        Date date = getDate(str, "yyyy-MM-dd HH:mm:ss");
        return date;
    }

    /**
     * 获取指定日期所在月的第一天
     *
     * @param date
     * @return
     */
    public static Date firstDayOfMonth(Date date) {
        int month = DateUtil.getMonth(date);
        int year = DateUtil.getYear(date);
        String strDate = year + "-" + month + "-" + "1";
        return getDate(strDate);
    }

    /**
     * @param date
     * @return
     * @Description: 获取指定日期最后一个月的数字（几号 28或30、31）
     * @author pangjianhai
     * @date 2014-8-19 下午2:09:46
     * @int
     */
    public static int lastDayNumOfMonth(Date date) {
        return getDay(lastDayOfMonth(date));
    }

    /**
     * @param d1
     * @param d2
     * @return
     * @Description: 两个日期相差多少年
     * @author pangjianhai
     * @date 2014-11-8 下午1:17:05
     * @int
     */
    public static int getSpan(Date d1, Date d2) {
        int n = 0;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(d1);
            c2.setTime(d2);
        } catch (Exception e) {
            logger.error("计算时间差出问题", e);
        }
        while (!c1.after(c2)) {
            n++;
            c1.add(Calendar.DATE, 1); // 比较天数，日期+1
        }
        n = n - 1;
        return n = n / 365;
    }

    public static Date getMaxDate(Date[] d) {
        Date maxDate = d != null && d.length > 0 ? d[0] : null;
        for (int i = 1; d != null && i < d.length; i++) {
            if (d[i] == null) continue;
            maxDate = maxDate == null || maxDate.before(d[i]) ? d[i] : maxDate;
        }
        return maxDate;
    }

    public static int compareTo(Date d1, Date d2) {
        if (d1 == null) {
            return -1;
        }
        if (d2 == null) {
            return 1;
        }
        return d1.compareTo(d2);
    }

    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 17位(yyyyMMddHHmmssSSS)长整数转为日期
     * @param l
     * @return
     */
    public static Date Long17ToDate(long l) {
        return new Date(l);
    }

    /**
     * 日期转为17位长整数(yyyyMMddHHmmssSSS)
     * @param date
     * @return
     */
    public static Long DateToLong17(DateTime date) {
        String d = date.toString("yyyyMMddHHmmssSSS");
        return Long.valueOf(d);
    }

    private static final SimpleDateFormat sdf17 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    /**
     * 日期转为17位长整数(yyyyMMddHHmmssSSS)
     * @param date
     * @return
     */
    public static Long DateToLong17(Date date) {
        String d = sdf17.format(date);
        return Long.valueOf(d);
    }




    public static void main(String[] args) {
//        String date = "1464779351000";
       String date = "1498630060";
        Date d =  ToDate(date);
        System.err.println(d);

//        long l = DateToLong17(DateTime.now());
//        Date dd = Long17ToDate(l);

        String dataStr = "20190318145654";
//        SimpleDateFormat sdf17 = new SimpleDateFormat("yyyyMMddHHmmss");
//        Date d = sdf17.parse(dataStr, new ParsePosition(0));
        Date d2 = ToDate(dataStr);
        System.err.println(d2);
    }


}
